﻿using CMSPages;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using t_TestingLibrary;
using System.ComponentModel.Composition;
using PaperRenderingInterfaces;
using System.IO;
using RenderEngineInterfaces;
using System.Collections.Generic;
using DeepZoomPivotConstructor.VisualLib;
using System.Drawing;
using System.Linq;
using t_PivotGeneratorTestHelpers;

namespace t_CMSPages
{
    
    
    /// <summary>
    ///This is a test class for ScanCMSEventDisplayPagesTest and is intended
    ///to contain all ScanCMSEventDisplayPagesTest Unit Tests
    ///</summary>
    [TestClass()]
    public class t_ScanCMSEventDisplayPages
    {
        [TestInitialize]
        public void TestSetup()
        {
            Utilities.MyClassInit();
        }

        [TestCleanup]
        public void TestCleanup()
        {
            Utilities.MyClassDone();
        }

        /// <summary>
        /// Load a file instead of accessing the web.
        /// </summary>
        [Export(typeof(IWebAccess))]
        private class LoadAFile : IWebAccess
        {
            public FileInfo theFileToLoad;
            public string UrlAskedFor { get; set; }

            public string GetUriAsString(Uri whatToLoad)
            {
                UrlAskedFor = whatToLoad.OriginalString;

                return LoadFile(theFileToLoad);
            }

            /// <summary>
            /// Load up the req' file.
            /// </summary>
            /// <param name="finfo"></param>
            /// <returns></returns>
            private string LoadFile(FileInfo finfo)
            {
                if (finfo.Exists)
                {
                    using (var reader = finfo.OpenText())
                    {
                        return reader.ReadToEnd();
                    }
                }
                else
                {
                    return null;
                }
            }


            public DateTime? GetLastModificationTime(Uri whatToCheck)
            {
                return DateTime.Now;
            }
        }

        [Export(typeof(IRenderEngine))]
        class RenderEngineDummy : IRenderEngine
        {
            public System.Threading.Tasks.Task<DeepZoomPivotConstructor.VisualLib.Visual> RenderToFuture(object what, Type[] disallowRenderPlugins = null, bool forceAsComposableUnit = false, bool requestComposableUnit = false)
            {
                throw new NotImplementedException();
            }

            List<string> whatWeSaw = new List<string>();
            public DeepZoomPivotConstructor.VisualLib.Visual Render(object what, Type[] disallowRenderPlugins = null, bool forceAsComposableUnit = false, bool requestComposableUnit = false)
            {
                Visual v = new VisualBitmapImage(1.0, 1.0, 100, Color.Black);
                if (forceAsComposableUnit)
                {
                    v.SetAsComposableUnit(null);
                }
                System.Diagnostics.Trace.WriteLine("Rendering " + what.ToString());
                whatWeSaw.Add(what.ToString());
                return v;
            }

            private void Trace(string p)
            {
                throw new NotImplementedException();
            }

            /// <summary>
            /// Did we see something as one of our requests?
            /// </summary>
            /// <param name="p"></param>
            /// <returns></returns>
            internal bool SawRequest(string p)
            {
                return whatWeSaw.Contains(p);
            }
        }

        [TestMethod]
        public void TestCtor()
        {
            ScanCMSEventDisplayPages p = new ScanCMSEventDisplayPages();
        }

        [TestMethod]
        public void TestSimpleWebPageRender()
        {
            ScanCMSEventDisplayPages target = new ScanCMSEventDisplayPages();
            var laf = new LoadAFile() { theFileToLoad = new FileInfo("..\\..\\..\\t_CMSPages\\cms7TeVEventDisplays.html") };
            Utilities.AddPart(laf);
            Utilities.AddPart(new RenderEngineDummy());
            Utilities.Compose(target);

            var result = target.Render("http://cms.web.cern.ch/cms/Media/Images/EventDisplays/7_0TeVCollisions/index.html");
            Assert.AreEqual("http://cms.web.cern.ch/cms/Media/Images/EventDisplays/7_0TeVCollisions/index.html", laf.UrlAskedFor, "URL didn't get mapped correctly");

            var results = result.ToArray();

            Assert.AreEqual(11, results.Count(), "Incorrect # of event displays found!");
            results.CheckNameAndDescription();
            Assert.IsTrue(results.All(r => r.Name.Contains("Event")), "Some don't contain a proper event name!");
        }

        [TestMethod]
        public void TestSplashEvents()
        {
            ScanCMSEventDisplayPages target = new ScanCMSEventDisplayPages();
            var laf = new LoadAFile() { theFileToLoad = new FileInfo("..\\..\\..\\t_CMSPages\\splash.html") };
            Utilities.AddPart(laf);
            Utilities.AddPart(new RenderEngineDummy());
            Utilities.Compose(target);

            var result = target.Render("http://cms.web.cern.ch/cms/Media/Images/EventDisplays/Beam2009/index.html");

            var results = result.ToArray();

            Assert.AreEqual(31, results.Count(), "Incorrect # of event displays found!");
            results.CheckNameAndDescription();
        }
    }
}
